package com.yu.shortlink.admin.common.biz.user;

/**
 * @Author yushiwang
 * @Date 2024/4/8 23:19
 * @PackageName:com.yu.shortlink.admin.common.biz.user
 * @ClassName: UserTransmitFilter
 * @Version 1.0
 * <p>
 * 用户信息传输过滤器
 *//*

 */
/**
 * 用户信息传输过滤器
 *
 */


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.yu.shortlink.admin.common.convention.result.Result;
import com.yu.shortlink.admin.common.convention.result.Results;
import com.yu.shortlink.admin.common.enums.UserErrorCodeEnum;
import com.yu.shortlink.admin.util.WebUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
public class UserTransmitFilter implements Filter {

    private final StringRedisTemplate redisTemplate;

    public static final List<String> IGNORE_URI = Lists.newArrayList(
            "/api/short-link/admin/v1/user/login",
            "/api/short-link/admin/v1/user/has-username",
            "/api/short-link/admin/v1/title",
            "/test/test12"
    );

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String requestURI = httpServletRequest.getRequestURI();
        if (IGNORE_URI.contains(requestURI) || (Objects.equals(requestURI, "/api/short-link/admin/v1/user") && Objects.equals(httpServletRequest.getMethod(), "POST"))) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        String username = httpServletRequest.getHeader("username");
        String token = httpServletRequest.getHeader("token");
        if (!StrUtil.isAllNotBlank(token)) {
            Result<Void> failure = Results.failure(UserErrorCodeEnum.USER_TOKEN_FAIL.code(), UserErrorCodeEnum.USER_TOKEN_FAIL.message());
            WebUtils.renderString((HttpServletResponse) servletResponse,JSON.toJSONString(failure));
            return;
        }
        Object userInfoJsonStr = null;
        try {
            userInfoJsonStr = redisTemplate.opsForHash().get("login_" + username, token);
            if (userInfoJsonStr == null){
                Result<Void> failure = Results.failure(UserErrorCodeEnum.USER_TOKEN_FAIL.code(), UserErrorCodeEnum.USER_TOKEN_FAIL.message());
                WebUtils.renderString((HttpServletResponse) servletResponse,JSON.toJSONString(failure));
                return;
            }
        } catch (Exception e) {
            Result<Void> failure = Results.failure(UserErrorCodeEnum.USER_TOKEN_FAIL.code(), UserErrorCodeEnum.USER_TOKEN_FAIL.message());
            WebUtils.renderString((HttpServletResponse) servletResponse,JSON.toJSONString(failure));
            return;
        }
            UserInfoDTO userInfoDTO = JSON.parseObject(userInfoJsonStr.toString(), UserInfoDTO.class);
            UserContext.setUser(userInfoDTO);
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            UserContext.removeUser();
        }
    }
}

